package com.qumitech.visualnovel.service.userLabel.impl;

import com.alibaba.fastjson.*;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.qumitech.visualnovel.service.userLabel.UserLabelService;
import com.google.common.collect.ImmutableMap;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.SecurityUtils;

import com.qumitech.visualnovel.model.dao.console.UserLabelConfDO;
import com.qumitech.visualnovel.model.dao.console.UserLabelOptionsDO;
import com.qumitech.visualnovel.model.dao.console.UserLabelGroupDO;
import com.qumitech.visualnovel.model.dao.console.ConsoleChannelConfig;
import com.qumitech.visualnovel.model.dao.app.GuildCfgDO;
import com.qumitech.visualnovel.model.dao.console.UserInfo;

import com.qumitech.visualnovel.datasource.console.UserLabelConfDOMapper;
import com.qumitech.visualnovel.datasource.console.UserLabelOptionsDOMapper;
import com.qumitech.visualnovel.datasource.console.UserLabelGroupDOMapper;
import com.qumitech.visualnovel.datasource.console.ConsoleChannelConfigMapper;
import com.qumitech.visualnovel.datasource.app.GuildCfgDOMapper;

import java.util.*;

@Service
public class UserLabelServiceImpl implements UserLabelService {
    @Autowired
    UserLabelConfDOMapper userLabelConfDOMapper;

    @Autowired
    UserLabelOptionsDOMapper userLabelOptionsDOMapper;

    @Autowired
    UserLabelGroupDOMapper userLabelGroupDOMapper;

    @Autowired
    ConsoleChannelConfigMapper channelConfigMapper;

    @Autowired
    private GuildCfgDOMapper guildCfgDOMapper;

    private String getSysUserName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser!=null ? activeUser.getUsername() : "";
    }

    @Override
    public BaseRsp getUserLabelConf() {
        List<UserLabelConfDO> confList = userLabelConfDOMapper.getList();
        List<UserLabelOptionsDO> optionList = userLabelOptionsDOMapper.getList();
        List<ConsoleChannelConfig> channelList = channelConfigMapper.getChannelOptions(null);
        List<GuildCfgDO> guildList = guildCfgDOMapper.getAllGuildNames();

        JSONObject result = new JSONObject();
        result.put("label", new JSONArray());
        result.put("option", new JSONArray());

        for (UserLabelConfDO item : confList) {
            JSONObject jsonItem = new JSONObject();
            jsonItem.put("id", item.getName());
            jsonItem.put("text", item.getLabel());
            jsonItem.put("valueType", item.getValueType());
            jsonItem.put("valueOption", item.getValueOption());
            jsonItem.put("typeOption", item.getTypeOption());
            result.getJSONArray("label").add(jsonItem);
        }

        for (UserLabelOptionsDO item : optionList) {
            JSONObject jsonItem = new JSONObject();
            jsonItem.put("id", item.getName());
            jsonItem.put("text", item.getLabel());
            jsonItem.put("options", JSON.parseArray(item.getOptions()));
            result.getJSONArray("option").add(jsonItem);
        }

        JSONObject channelItem = new JSONObject();
        channelItem.put("id", "channel");
        channelItem.put("text", "渠道");
        channelItem.put("options", new JSONObject());
        result.getJSONArray("option").add(channelItem);
        for (ConsoleChannelConfig item : channelList) {
            String mainChannel = item.getName();
            String subChannel = item.getSecondName();
            subChannel = (subChannel!=null && subChannel.length()>0) ? subChannel : mainChannel;
            if (!channelItem.getJSONObject("options").containsKey(mainChannel)) {
                channelItem.getJSONObject("options").put(mainChannel, new JSONArray());
            }
            channelItem.getJSONObject("options").getJSONArray(mainChannel).add(subChannel);
        }

        JSONObject guildItem = new JSONObject();
        guildItem.put("id", "guild");
        guildItem.put("text", "工会");
        guildItem.put("options", new JSONArray());
        result.getJSONArray("option").add(guildItem);
        for (GuildCfgDO item : guildList) {
            JSONObject jsonItem = new JSONObject();
            jsonItem.put("id", item.getGuildId());
            jsonItem.put("text", item.getName());
            guildItem.getJSONArray("options").add(jsonItem);
        }

        return new BaseRsp(ResultEnum.SUCCESS, result);
    }

    @Override
    public BaseRsp getUserLabelGroupNames() {
        JSONArray result = new JSONArray();
        List<UserLabelGroupDO> list = userLabelGroupDOMapper.getNames();
        for (UserLabelGroupDO item : list) {
            JSONObject jsonItem = new JSONObject();
            jsonItem.put("id", item.getName());
            jsonItem.put("text", item.getName());
            result.add(jsonItem);
        }
        return new BaseRsp(ResultEnum.SUCCESS, result);
    }

    @Override
    public BaseRsp getUserLabelGroup(String name, String startTime, String endTime, int offset, int limit) {
        int count = userLabelGroupDOMapper.getCount(name, startTime, endTime);
        List<UserLabelGroupDO> list = userLabelGroupDOMapper.getList(name, startTime, endTime, offset, limit);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "list", list));
    }

    @Override
    public BaseRsp addUserLabelGroup(JSONObject body) {
        UserLabelGroupDO record = new UserLabelGroupDO();
        record.setOperator(getSysUserName());
        record.setName(body.getString("name"));
        record.setComments(body.getString("comments"));
        record.setLabels(body.getString("labels"));
        userLabelGroupDOMapper.insertSelective(record);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @Override
    public BaseRsp modUserLabelGroup(Long id, JSONObject body) {
        UserLabelGroupDO record = new UserLabelGroupDO();
        record.setId(id);
        record.setOperator(getSysUserName());
        record.setName(body.getString("name"));
        record.setComments(body.getString("comments"));
        record.setLabels(body.getString("labels"));
        record.setState(0);
        record.setRunInterval(0);
        userLabelGroupDOMapper.updateByPrimaryKeySelective(record);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @Override
    public BaseRsp delUserLabelGroup(Long id) {
        userLabelGroupDOMapper.deleteByPrimaryKey(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
}
